package org.edu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.edu.model.Emp;
import org.edu.model.User;
import org.edu.service.EmpService;
import org.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
/**
 * <h2>部门Action</h2>
 * <p>已在方法上注明方法作用</p>
 * @author 谢圣强
 * @version 2.0
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/emp")
public class EmpAction extends ActionSupport implements RequestAware  {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 9070305852597893618L;
	private static final Logger logger = Logger.getLogger(EmpAction.class);
	private Map<String, Object> request;
	private String empid;
	private int currentPage;//
	private String accountarray; 
	
	@Autowired
	private EmpService empService;
	@Autowired
	private UserService userService;
	
	@Action(value="emp",results={@Result(name="success",location="/examples/Staff.jsp"),
			@Result(name="error",location="/examples/msg.jsp")})
	public String emp(){
		logger.info("获取部门"+empid+"人员信息");
		try{
			Emp e= empService.show(Integer.parseInt(empid));
			if(null!=e){
				request.put("emp", e);
				request.put("result", empService.pageResault(e, currentPage));
				
			}else{
				request.put("tip", "获取信息失败");//错误跳转
				logger.info("获取部门"+empid+"人员信息失败");
				return ERROR;
			}
		}catch (Exception e){
			e.printStackTrace();
			request.put("tip", "获取信息失败");//错误跳转
			logger.info("获取部门"+empid+"人员信息失败");
			return ERROR;
		}
		return SUCCESS;
	}
	/**
	 * 获取部门信息树
	 */
	@Action(value="loaduser",results={@Result(name="success",location="/examples/userinfo.jsp"),
			@Result(name="error",location="/examples/msg.jsp")})
	public String loaduser(){
		logger.info("获取"+accountarray+"信息");
		try{
			User u=userService.getUser(accountarray);
			if(null!=u){
				//System.out.println("EmpAction"+u.getSroleid());
				request.put("userreq", u);
			}else{
				request.put("tip", "获取信息失败");//错误跳转
				logger.info("获取用户："+accountarray+"失败");
				return ERROR;
			}
		}catch(Exception e){
			e.printStackTrace();
			request.put("tip", "获取信息失败");//错误跳转
			logger.info("获取用户："+accountarray+"失败");
			return ERROR;
		}
		return SUCCESS;
	}
	/**
	 * 获取部门信息树
	 */
	@Action(value="loademp")
	public void loademp(){
		HttpServletResponse response = ServletActionContext.getResponse();
		// 以下代码从JSON.java中拷过来的
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(empService.list().toString());
		List<Object> l= new ArrayList<Object>();
		l.addAll(empService.list());
		l.addAll(userService.list());
		
		Emp e= new Emp();
		e.setEmpno(0);
		e.setEmpname("XX软件股份有限公司");
		e.setPno(-1);
		l.add(e);
		//System.out.println(l);
		out.println(l.toString());
		logger.info("获取全部部门列表");
		//request.put("emplist", empService.list());
	}
	
	@Action(value="emptytime")
	public void emptyTime(){
		HttpServletResponse response = ServletActionContext.getResponse();
		// 以下代码从JSON.java中拷过来的
		//HttpSession session =  ServletActionContext.getRequest().getSession();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		

			logger.info("清空"+accountarray+"帐号时长");
			userService.emptyTime(accountarray);//,((User)session.getAttribute("user")).getAccount());
			out.println("succes");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			
			return ;
		}
		
		//request.put("emplist", empService.list());
	}
	
	@Action(value="passdefault")
	public void passDefault(){
		HttpServletResponse response = ServletActionContext.getResponse();
		// 以下代码从JSON.java中拷过来的
		//HttpSession session =  ServletActionContext.getRequest().getSession();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		

			logger.info("还原"+accountarray+"帐号密码");
			userService.passDefault(accountarray);//,((User)session.getAttribute("user")).getAccount());
			out.println("succes");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			
			return ;
		}
		
		//request.put("emplist", empService.list());
	}
	public Map<String, Object> getRequest() {
		return request;
	}
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	public String getEmpid() {
		return empid;
	}
	public void setEmpid(String empid) {
		this.empid = empid;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getAccountarray() {
		return accountarray;
	}

	public void setAccountarray(String accountarray) {
		this.accountarray = accountarray;
	}
	

}
